package com.yxhl.zoume.core.func.map;

import android.content.Context;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Tip;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.func.map.info.PoiInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xboy on 15/12/6.
 */
public class InputTipsTask implements Inputtips.InputtipsListener {

    private static InputTipsTask mInputTipTask;
    private Inputtips mInputTips;
    private BaseAdapter mAdapter;

    private InputTipsTask(Context context) {
        mInputTips = new Inputtips(context, this);
    }

    public static InputTipsTask getInstance(Context context, BaseAdapter adapter) {
        if (mInputTipTask == null) {
            mInputTipTask = new InputTipsTask(context);
        }

        mInputTipTask.setRecommandAdapter(adapter);
        return mInputTipTask;
    }

    public void setRecommandAdapter(BaseAdapter adapter) {
        mAdapter = adapter;
    }

    public void searchTips(String keyWord, String city) {
        try {
            mInputTips.requestInputtips(keyWord, city);
        } catch (AMapException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetInputtips(List<Tip> tips, int resultCode) {

        if (resultCode == 0 && tips != null) {
            ArrayList<PoiInfo> positions = new ArrayList<PoiInfo>();
            for (Tip tip : tips) {

                if (tip.getPoint() != null) {

                    double lat = tip.getPoint().getLatitude();
                    double lng = tip.getPoint().getLongitude();
                    String addName = tip.getName();
                    String address = tip.getDistrict();
                    String city = "";//暂无
                    String cityCode = "";//暂无
                    String aCode = tip.getAdcode();
                    positions.add(new PoiInfo(lat, lng, addName, address, city, cityCode, aCode));
                }
            }

            // TODO: 16/3/21 RRD Xboy ?设置数据
//            mAdapter.setData(positions);
//            mAdapter.notifyDataSetChanged();
        }
        //TODO 可以根据app自身需求对查询错误情况进行相应的提示或者逻辑处理
    }
}
